package com.cqeec.bootdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqeec.bootdemo.common.CustomException;
import com.cqeec.bootdemo.dto.DishDto;
import com.cqeec.bootdemo.entity.Dish;
import com.cqeec.bootdemo.entity.DishFlavor;
import com.cqeec.bootdemo.entity.SetmealDish;
import com.cqeec.bootdemo.mapper.DishMapper;
import com.cqeec.bootdemo.service.DishFlavorService;
import com.cqeec.bootdemo.service.DishService;

import com.cqeec.bootdemo.service.SetmealDishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DishServiceImp extends ServiceImpl<DishMapper, Dish>
        implements DishService {
    /**
     * 新增dish表与dishFlavor表的数据
     *
     * @param dishDto
     */
    @Autowired
    DishFlavorService dishFlavorService;

    @Override
    @Transactional
    public void savedishWithFlavors(DishDto dishDto) {
        //先保存dish表的数据
        this.save(dishDto);
        //再保存dishFlavor表的数据
        //1.foreach lambda表达式
//        dishDto.getFlavors().forEach(item -> {
//            item.setDishId(dishDto.getId());
//        });
        //2.stream流式处理
//        dishDto.getFlavors().stream().map(item -> {
//            item.setDishId(dishDto.getId());
//            return item;
//        }).collect(Collectors.toList());
        //3.foreach循环
        for(DishFlavor flavor:dishDto.getFlavors()){
            flavor.setDishId(dishDto.getId());
        }
        dishFlavorService.saveBatch(dishDto.getFlavors());
    }

    /**
     * 根据id查询dish表和dishFlavor表相关的数据
     * @param id
     * @return
     */
    @Override
    public DishDto getWithFlavor(Long id) {
        DishDto dishDto=new DishDto();
        Dish dish = this.getById(id);
        BeanUtils.copyProperties(dish, dishDto);
        //flavor相关的数据
        //根据dish_id字段查询dishFlavor表的数据
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, id);
        List<DishFlavor> flavorList=dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(flavorList);
        return dishDto;
    }
//修改
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //1.先修改dish表的数据
        this.updateById(dishDto);
        //2.dishFlavor表的数据
        //2.1  先删除
        LambdaQueryWrapper<DishFlavor> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(wrapper);
        //2.2  再新增
       for(DishFlavor flavor:dishDto.getFlavors()){
            flavor.setDishId(dishDto.getId());
       }
       //批量操作
       dishFlavorService.saveBatch(dishDto.getFlavors());
    }
@Autowired
SetmealDishService setMealDishService;
    @Override
    @Transactional
    public void removeWithFlavor(List<Long> ids) {
      //如果这个菜品在套餐里，不允许删除
        LambdaQueryWrapper<SetmealDish> wrapper=new LambdaQueryWrapper<>();
        wrapper.in(SetmealDish::getDishId, ids);
        long count=setMealDishService.count(wrapper);
        if(count>0){
            throw new CustomException("删除的菜品中包含套餐数据，不能删除");
        }
        //如果这个菜在启售状态，也不许删除
        LambdaQueryWrapper<Dish> wrapper1=new LambdaQueryWrapper<>();
        wrapper1.in(Dish::getId, ids).eq(Dish::getStatus, 1);
        long count1=this.count(wrapper1);
        if(count1>0){
            throw new CustomException("删除的菜品是启售状态，不能删除");
        }
        //可以删除,删除dish表数据
        this.removeByIds(ids);
        //删除dishFlavor表相关数据
        LambdaQueryWrapper<DishFlavor> wrapper2=new LambdaQueryWrapper<>();
        wrapper2.in(DishFlavor::getDishId, ids);
        dishFlavorService.remove(wrapper2);
    }


}
